<?php
/**
 * Created by PhpStorm.
 * User: qishengqiang
 * Date: 2022-10-13
 * Time: 01:09
 */

namespace app\admin\controller;


use app\admin\BaseController;

class GdzyzSchoolController extends BaseController
{
    public function listsAction()
    {
        try {
            $post = $this->get;

            $params['order'] = ['schoolId'=>'desc'];

            if(!empty($post['schoolName'])){
                $params['where'][] = ['schoolName','like','%'.$post['schoolName'].'%'];
            }

            $schools = $this->app->gdzyzSchoolService->getLists($params, $this->page, $this->pagesize);

            $data['lists'] = [];
            foreach($schools as $key => $vo){
                $data['lists'][] = [
                    'schoolId' => $vo->schoolId,
                    'schoolName' => $vo->schoolName,
                    'missionId' => $vo->missionId,
                    'subject' => $vo->mission?$vo->mission->subject:'',
                    'createdAt' => $vo->createdAt,
                ];
            }

            $data['totalInfo'] = $this->app->utilService->getMeta($schools);

            return json()->data(['code' => 200, 'msg' => '查询成功', 'data' => $data]);

        } catch (\Exception $e) {
            return json()->data(['code' => 500, 'msg' => $e->getMessage(), 'data' => []]);
        }
    }

    public function detailAction($id)
    {
        try {
            $school = $this->app->gdzyzSchoolService->getDetail($id);
            $data = $school->toArray();
            $data['subject'] = $school->mission?$school->mission->subject:'';

            return json()->data(['code' => 200, 'msg' => '查询成功', 'data' => $data]);

        } catch (\Exception $e) {
            return json()->data(['code' => 500, 'msg' => $e->getMessage(), 'data' => []]);
        }
    }

    public function addAction()
    {
        try {
            $params = $this->post;

            $this->app->gdzyzSchoolService->create($params);

            return json()->data(['code' => 200, 'msg' => '处理成功', 'data' => []]);

        } catch (\Exception $e) {
            return json()->data(['code' => 500, 'msg' => $e->getMessage(), 'data' => []]);
        }
    }

    public function updateAction($id)
    {
        try {
            $school = $this->app->gdzyzSchoolService->getDetail($id);

            $this->app->gdzyzSchoolService->update($school, $this->post);

            return json()->data(['code' => 200, 'msg' => '处理成功', 'data' => []]);

        } catch (\Exception $e) {
            return json()->data(['code' => 500, 'msg' => $e->getMessage(), 'data' => []]);
        }
    }

    public function deleteAction($id)
    {
        try {
            $school = $this->app->gdzyzSchoolService->getDetail($id);

            $this->app->gdzyzSchoolService->delete($school);

            return json()->data(['code' => 200, 'msg' => '处理成功', 'data' => []]);

        } catch (\Exception $e) {
            return json()->data(['code' => 500, 'msg' => $e->getMessage(), 'data' => []]);
        }
    }
}